﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Physis.Client.Models.Insurer {

    public class BenefitPlanModel  : Configuration.ConfigurationItemModel {
                
        #region Private Properties

        private Core.Insurer.BenefitPlan benefitPlan = null;

        #endregion


        #region Public Properties

        public override Int64 ConfigurationItemId { set { BenefitPlanId = value; } }

        public override Core.CoreConfigurationObject ConfigurationItem { get { return benefitPlan; } }


        public Int64 BenefitPlanId {

            set {

                benefitPlan = Application.Core.Insurer.BenefitPlan (value);

                if (benefitPlan == null) {

                    benefitPlan = new Core.Insurer.BenefitPlan (Application);

                }

            }

        }

        public Core.Insurer.BenefitPlan BenefitPlan { get { return benefitPlan; } set { benefitPlan = value; } }


        public System.Web.Mvc.SelectList ProgramSelectList {

            get {

                List<Core.Insurer.Program> programs;

                programs = (from currentProgram in Application.Core.Insurer.Programs (false)

                            where ((currentProgram.Enabled) || (currentProgram.Id == BenefitPlan.ProgramId)) // ACTIVE OR IN USE 

                            select currentProgram).ToList ();

                return new System.Web.Mvc.SelectList (programs, "Id", "Name");

            }

        }

        public System.Web.Mvc.SelectList InsuranceTypeSelectList {

            get {

                List<Core.Insurer.InsuranceType> insuranceTypes;

                insuranceTypes = (from currentInsuranceType in Application.Core.Insurer.InsuranceTypes (false)

                                where ((currentInsuranceType.Enabled) || (currentInsuranceType.Id == BenefitPlan.InsuranceTypeId)) // ENABLED OR IN USE ONLY 

                                select currentInsuranceType).ToList ();

                return new System.Web.Mvc.SelectList (insuranceTypes, "Id", "Name");

            }

        }

        #endregion 


        #region Constructors

        public BenefitPlanModel (Int64 forBenefitPlanId) {

            BenefitPlanId = forBenefitPlanId;

            return;

        }

        public BenefitPlanModel (System.Collections.Specialized.NameValueCollection form) {

            UpdateValues (form);

            return;

        }

        #endregion 


        #region Public Methods

        public override void UpdateValues (System.Collections.Specialized.NameValueCollection form) {

            base.UpdateValues (form); // BASE RELOADS THE ORIGINAL OBJECT TYPE


            // ALLOW UPDATING OF READ-ONLY PROPERTIES FOR NEW PROGRAMS

            if (BenefitPlan.Id == 0) {

                BenefitPlan.ProgramId = Data.Readers.FormReader.Int64FromForm (form, "BenefitPlan.ProgramId");

                BenefitPlan.InsuranceTypeId = Data.Readers.FormReader.Int64FromForm (form, "BenefitPlan.InsuranceTypeId");

            }

            else {

                // RESET READ-ONLY PROPERTIES FOR EXISTING PROGRAMS 

                BenefitPlan.Name = Application.Core.Insurer.BenefitPlan (BenefitPlan.Id, false).Name;

            }


            // EDITABLE PROPERTIES FOR NEW AND EXISTING 

            BenefitPlan.UniqueId = Data.Readers.FormReader.StringFromForm (form, "BenefitPlan.UniqueId");

            BenefitPlan.LegacyId = Data.Readers.FormReader.StringFromForm (form, "BenefitPlan.LegacyId");


            return;

        }

        #endregion 

    }

}